<?php
class ContactController extends Core_Controller
{
    public function indexAction()
    {
        if(count($this->_http->getArvs())>0) throw new Exception("404 not found",404);
        if(! isset($_SESSION['admin_id'])) $this->redirect('/admin');
        $this->_view->title = "Quản lý các liên hệ";
        $this->_view->activeClass = "contact";
        
        $modelContact = new Admin_Model_Contact();
        $this->_view->list = $modelContact->getContacts();
    }

        
    public function pagingAction()
    {
    	if(! isset($_SESSION['admin_id'])) $this->redirect('/admin');
    	$this->_view->layout(false);
    	
    	if(count($this->_http->getArvs()) != 1) 
    		$this->_view->status = 0;
    	else 
    	{
    		$p = $this->_http->getParam('p');
	        if($p == null) 
	        	$this->_view->status = 0;
	        else 
	        {
	        	$p = intval($p);
	        		        
	       		$modelContact = new Admin_Model_Contact();
	        	$listContact = $modelContact->getContacts();
	        
		        if($listContact != null)
		        {
		        	$paginator = new Utilities_Paginator($listContact);
		        	$paginator->setItemCountPerPage(15);
		        	$paginator->setCurrentPageNumber($p);
		        	$this->_view->list = $paginator->getResult();
		        	$this->_view->paginator = $paginator;
		        	$this->_view->indexStart = ($paginator->getCurrentPageNumber()-1) * $paginator->getItemCountPerPage() + 1;
		        	$this->_view->status = 1;
		        }
		        else 
		        	$this->_view->status = 0;
	        }
    	}
    	
    }
    
    
    public function readAction()
    {
        if(count($this->_http->getArvs()) != 1 ) throw new Exception("404 not found",404);
        $id = $this->_http->getParam('id');
        if($id == null) throw new Exception("404 not found",404);
        if(! isset($_SESSION['admin_id'])) $this->redirect('/admin');
        $this->_view->title = "Đọc liên hệ";
        $this->_view->activeClass = "contact";
        
        $modelContact = new Admin_Model_Contact();
        $result = $modelContact->getContact($id);
        if($result == null) throw new Exception("404 not found",404);
        if($result[0]['unread'] == 1)
              $modelContact->updateUnread($id);
        $this->_view->result = $result;
    }
    
    public function deleteAction()
    {
        if(count($this->_http->getArvs()) != 1 ) throw new Exception("404 not found",404);
        $id = $this->_http->getParam('id');
        if($id == null) throw new Exception("404 not found",404);
        if(! isset($_SESSION['admin_id'])) $this->redirect('/admin');
        
        $modelContact = new Admin_Model_Contact();
        $ok = $modelContact->deleteContact($id);
        $this->redirect('/admin/contact');
    }
}